'use strict';

const {
	Controller
} = require('uni-cloud-router');
module.exports = class DynamicsController extends Controller {
	// 获取动态列表
	async getDynamicsList() {
		const dbJQL = uniCloud.databaseForJQL({
			event: this.ctx.event,
			context: this.ctx.context
		});
		
		const res = {
			code: 200,
			data: {},
			message: '获取动态列表成功'
		}

		const {
			page,
			pageSize,
			title,
		} = this.ctx.data;
		
		console.log('page->', page, 'pageSize->', pageSize, 'title->', title);
		
		let dbres = await dbJQL.collection('dynamics')
		.where(`${new RegExp(title, 'i')}.test(title)`)
		.skip((page - 1) * pageSize)
		.limit(pageSize)
		.get({
			getCount: true
		})

		console.log('获取店铺动态列表--------->', dbres);
		res.data = dbres.data;
		res.total = dbres.count;

		return res;
	}
	// 获取动态详情
	async getDynamicsDetail() {
		const dbJQL = uniCloud.databaseForJQL({
			event: this.ctx.event,
			context: this.ctx.context
		});
		
		const res = {
			code: 200,
			data: {},
			message: '获取动态详情成功'
		}
		
		const {
			id
		} = this.ctx.data;
		
		if (!id) {
			res.code = 400;
			res.message = '参数错误';
			return res;
		}
		
		let dbres = await dbJQL.collection('dynamics')
		.where({
			_id: id
		})
		.get()
		
		console.log('===================>', dbres);
		
		res.data = dbres.data[0];
		
		return res;
	}
	// 新增动态
	async addDynamics() {
		const dbJQL = uniCloud.databaseForJQL({
			event: this.ctx.event,
			context: this.ctx.context
		});
		
		const res = {
			code: 200,
			data: {},
			message: ''
		}
		
		const {
			title,
			content,
			imgs
		} = this.ctx.data;
		
		console.log('title->', title, 'content', content, 'imgs->', imgs);
		
		if (!title || !content || !imgs) {
			res.code = 400;
			res.message = '参数错误';
			return res;
		}
		
		let dbres = await dbJQL.collection('dynamics').add({
			title,
			content,
			imgs,
			create_time: new Date().getTime(),
			update_time: new Date().getTime()
		})
		
		res.message = '新增动态成功';
		
		return res;
	}
	// 删除动态
	async deleteDynamics() {
		const dbJQL = uniCloud.databaseForJQL({
			event: this.ctx.event,
			context: this.ctx.context
		});
		
		const res = {
			code: 200,
			data: {},
			message: ''
		}
		
		const {
			id
		} = this.ctx.data;
		
		if (!id) {
			res.code = 400;
			res.message = '参数错误';
			return res;
		}
		
		let dbres = await dbJQL.collection('dynamics').where({
			_id: id
		}).remove();
		
		res.message = '删除动态成功';
		
		return res;
	}
	// 修改动态
	async updateDynamics() {
		const dbJQL = uniCloud.databaseForJQL({
			event: this.ctx.event,
			context: this.ctx.context
		});
		
		const res = {
			code: 200,
			data: {},
			message: ''
		}
		
		const {
			id,
			title,
			content,
			imgs
		} = this.ctx.data;
		
		if (!id || !title || !content || !imgs) {
			res.code = 400;
			res.message = '参数错误';
			return res;
		}
		
		let dbres = await dbJQL.collection('dynamics').where({
			_id: id
		}).update({
			title,
			content,
			imgs,
			update_time: new Date().getTime()
		});
		
		res.message = '修改动态成功';
		
		return res;
	}
};